package com.gpframework.module.ctn.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.ctn.domain.*;
import com.gpframework.module.ctn.domain.req.GpSmsSendLogAdd;
import com.gpframework.module.ctn.domain.vo.GpSmsSendLogExcel;
import com.gpframework.module.ctn.domain.vo.GpSmsSendLogVo;
import com.gpframework.module.ctn.mapper.GpSmsSendLogMapper;
import com.gpframework.module.ctn.service.IGpSmsSendLogService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName GpSmsSendLog
 * @Author gpframework
 * @Date 2024-01-09
 * @Version 1.0.0
 * @Description 货柜到市场发送短信Service业务层处理
 */
@Service
public class GpSmsSendLogServiceImpl extends ServiceImpl<GpSmsSendLogMapper, GpSmsSendLog> implements IGpSmsSendLogService {

    @Autowired
    private GpCtnOrderServiceImpl gpCtnOrderService;

    @Autowired
    private GpSackInformationServiceImpl gpSackInformationService;
    @Autowired
    private GpPackagesInformationServiceImpl gpPackagesInformationService;


    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2024-01-09
     * @Param [lqw]
     * @Return List<GpSmsSendLogVo>
     */
    @Override
    public List<GpSmsSendLogVo> listVo(QueryWrapper<GpSmsSendLog> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2024-01-09
     * @Param [id]
     * @Return GpSmsSendLogVo
     */
    @Override
    public GpSmsSendLogVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public List<GpSmsSendLogExcel> gpSmsSendLogImport(List<GpSmsSendLogExcel> excelList, Long brandId, Long storeId) {
        //导入失败的列表
        List<GpSmsSendLogExcel> failureList = new ArrayList<>();
        //货柜到市场发送短信
        GpSmsSendLog gpSmsSendLog;

        for (int i = 0; i < excelList.size(); i++) {
            GpSmsSendLogExcel excel = excelList.get(i);
            if (StringUtils.isNull(excel)) {
                excel = new GpSmsSendLogExcel();
                excel.setSubscript(i+1);
                excel.setMsg("读取数据失败");
                failureList.add(excel);
                continue;
            }
            gpSmsSendLog = Convert.convert(new TypeReference<GpSmsSendLog>() {}, excel);
            this.save(gpSmsSendLog);
        }
        return failureList;
    }

    @Override
    public boolean addBatch(GpSmsSendLogAdd gpSmsSendLogAdd) {
        List<GpSmsSendLog> gpSmsSendLogList = gpSmsSendLogAdd.getGpSmsSendLogList();
        if (CollectionUtils.isNotEmpty((gpSmsSendLogList))){
            for (int i = 0; i < gpSmsSendLogList.size(); i++) {
                GpSmsSendLog gpSmsSendLog = gpSmsSendLogList.get(i);
                if (CollectionUtils.isNotEmpty(gpSmsSendLog.getPackagesInformationList())){
                    //发送打包货柜到市场信息列表
                    this.sendSmsPackagesInformations(gpSmsSendLog);
                }
                if (CollectionUtils.isNotEmpty(gpSmsSendLog.getSackInformationList())){
                    //发送打包货柜到市场信息列表
                    this.sendSmsSackInformations(gpSmsSendLog);
                }

            }
        }

        return true;
    }

    private void sendSmsPackagesInformations(GpSmsSendLog gpSmsSendLog) {
        for (GpPackagesInformation gpPackagesInformation : gpSmsSendLog.getPackagesInformationList()) {
            GpSmsSendLog smsSendLog = Convert.convert(new TypeReference<GpSmsSendLog>() {}, gpPackagesInformation);
            smsSendLog.setSmsToMarketDate(gpPackagesInformation.getSmsToMarketDate());
            smsSendLog.setSendStatus("1");
            if (gpPackagesInformation.getSendSmsId()!=null){
                smsSendLog.setId(gpPackagesInformation.getSendSmsId());
            }else {
                smsSendLog.setId(null);
            }
            this.saveOrUpdate(smsSendLog);

            //更新打包信息是否发送短信
            GpPackagesInformation packagesInformation = gpPackagesInformationService.getById(gpPackagesInformation.getId());
            packagesInformation.setSendStatus("1");
            packagesInformation.setSmsToMarketDate(gpPackagesInformation.getSmsToMarketDate());
            packagesInformation.setSendSmsId(smsSendLog.getId());
            gpPackagesInformationService.updateById(packagesInformation);

            //更新整个装柜发送信息情况
            int sendIng = gpPackagesInformationService.count(new QueryWrapper<GpPackagesInformation>().eq("receipt_id", packagesInformation.getReceiptId()).eq("ctn_order_id", packagesInformation.getCtnOrderId()));
            int sended = gpPackagesInformationService.count(new QueryWrapper<GpPackagesInformation>().eq("receipt_id", packagesInformation.getReceiptId()).eq("ctn_order_id", packagesInformation.getCtnOrderId()).eq("send_status", 1));
            if (sendIng==sended){
                //全部发送
                GpCtnOrder gpCtnOrdered = gpCtnOrderService.getById(gpSmsSendLog.getId());
                gpCtnOrdered.setSendStatus("1");
                gpCtnOrderService.updateById(gpCtnOrdered);
            }
            if (sendIng<sended){
                //部分发送
                GpCtnOrder gpCtnOrdered = gpCtnOrderService.getById(gpSmsSendLog.getId());
                gpCtnOrdered.setSendStatus("2");
                gpCtnOrderService.updateById(gpCtnOrdered);
            }

        }

    }

    private void sendSmsSackInformations(GpSmsSendLog gpSmsSendLog) {
        for (GpSackInformation gpSackInformation : gpSmsSendLog.getSackInformationList()) {
            GpSmsSendLog smsSendLog = Convert.convert(new TypeReference<GpSmsSendLog>() {}, gpSackInformation);
            smsSendLog.setSmsToMarketDate(gpSackInformation.getSmsToMarketDate());
            smsSendLog.setSendStatus("1");
            // 判断新增还是修改
            if (gpSackInformation.getSendSmsId()!=null){
                smsSendLog.setId(gpSackInformation.getSendSmsId());
            }else {
                smsSendLog.setId(null);
            }
            this.saveOrUpdate(smsSendLog);

            //更新打包信息是否发送短信
            GpSackInformation sackInformation = gpSackInformationService.getById(gpSackInformation.getId());
            sackInformation.setSendStatus("1");
            sackInformation.setSendSmsId(smsSendLog.getId());
            sackInformation.setSmsToMarketDate(gpSackInformation.getSmsToMarketDate());
            gpSackInformationService.updateById(sackInformation);

            //更新整个装柜发送信息情况
            int sendIng = gpSackInformationService.count(new QueryWrapper<GpSackInformation>().eq("receipt_id", sackInformation.getReceiptId()).eq("ctn_order_id", sackInformation.getCtnOrderId()));
            int sended = gpSackInformationService.count(new QueryWrapper<GpSackInformation>().eq("receipt_id", sackInformation.getReceiptId()).eq("ctn_order_id", sackInformation.getCtnOrderId()).eq("send_status", 1));
            if (sendIng==sended){
                //全部发送
                GpCtnOrder gpCtnOrder = gpCtnOrderService.getById(gpSmsSendLog.getId());
                gpCtnOrder.setSendStatus("1");
                gpCtnOrderService.updateById(gpCtnOrder);
            }
            if (sendIng<sended){
                //部分发送
                GpCtnOrder gpCtnOrder = gpCtnOrderService.getById(gpSmsSendLog.getId());
                gpCtnOrder.setSendStatus("2");
                gpCtnOrderService.updateById(gpCtnOrder);
            }

        }
    }


}
